package com.bgds.sys.module.controller;


import com.alibaba.fastjson.JSONObject;
import com.bgds.sys.common.pagevo.Result;
import com.bgds.sys.common.pagevo.SeriesVo;
import com.bgds.sys.common.utils.DateUtils;
import com.bgds.sys.common.utils.ResultUtil;
import com.bgds.sys.influx.service.InfluxService;
import com.bgds.sys.module.entity.DeviceDynamicAttr;
import com.bgds.sys.module.entity.dto.AlarmLogDto;
import com.bgds.sys.module.service.IAlarmLogService;
import com.bgds.sys.module.service.IDeviceDynamicAttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lj
 */
@Slf4j
@RestController
@Api(tags = "主电机温度监测接口")
@RequestMapping("/mainMotor")
public class MainMotorController {
    @Autowired
    private IDeviceDynamicAttrService iDeviceDynamicAttrService;
    @Autowired
    private IAlarmLogService iAlarmLogService;
    @Autowired
    private InfluxService influxService;


    @RequestMapping(value = "/temperature", method = RequestMethod.GET)
    @ApiOperation(value = "获取主电机温度监测数据",notes = "获取主电机温度监测数据",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceCode", dataType = "String", required = true, value = "设备编号(code)"),
            @ApiImplicitParam(name = "startTime",  value = "起始时间"),
            @ApiImplicitParam(name = "endTime",  value = "结束时间"),
    })

    public Result<Map<String,Object>> getDataElectricTemperature(String deviceCode,Long startTime,Long endTime){
        Map<String,Object> map=new HashMap<>(10);
        List<DeviceDynamicAttr> deviceDynamicAttrs=iDeviceDynamicAttrService.listByDeviceCode(deviceCode,"main_motor");
        List<AlarmLogDto> alarmLogs=iAlarmLogService.listByParam(deviceCode, startTime, endTime);
        //分组
        Map<String, List<DeviceDynamicAttr>>  groupAttrs=deviceDynamicAttrs.stream().filter(item-> item.getGroupCode()!=null).collect(Collectors.groupingBy(DeviceDynamicAttr::getGroupCode, LinkedHashMap::new,Collectors.toList()));
        Map<String,List<SeriesVo>> listMap=new HashMap<>(2);
        for(Map.Entry<String, List<DeviceDynamicAttr>> entry : groupAttrs.entrySet()){
            List<SeriesVo>  chartData= influxService.queryByForMap(deviceCode,startTime,endTime,entry.getValue());
            if(entry.getKey().equals("current")){
                    chartData.forEach(item -> {
                        Double d=item.getValue();
                        if(item.getName().contains("额定")){
                            if(item.getName().contains("飞剪")){
                                d=480.00;
                            }else  if ((item.getName().contains("R1")||item.getName().contains("R2"))){
                                d=776.00;
                            }else  {
                                d=1500.00;
                            }
                            item.setValue(d);
                        }
                    });
            }
            listMap.put(entry.getKey(),chartData);
        }
        map.put("data",listMap);
        map.put("deviceDynamicAttrs",deviceDynamicAttrs);
        map.put("alarmLogs",alarmLogs);
        return new ResultUtil<Map<String,Object>>().setData(map);
    }


}

